﻿#ifndef SIDEBROWSER_H
#define SIDEBROWSER_H

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QDebug>
#include <QResizeEvent>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QStackedWidget>
#include <map>

#include "CustomTabview.h"

class OneItem : public QWidget
{
    Q_OBJECT
public:
    OneItem(const QString& strTitle, const QString& strIcon, const uint nIndex, QWidget* parent = nullptr);
    virtual ~OneItem() {}

    void setInfo(const QString& strTitle, const QString& strIcon, const uint nIndex);
    void refresh();
    void setNoSelectStatus();
    void setSelectedStatus();

signals:
    void signal_selected(uint nIndex, QString strTitle);

protected:
    void mousePressEvent(QMouseEvent* event) override;
    bool eventFilter(QObject* obj, QEvent* event) override;
private:
    QLabel *m_pStatus;
    QLabel *m_pLogo;
    QLabel *m_pTitle;
    bool    m_bFresh;
    QString m_strIcon;
    QString m_strTitle;
    uint    m_nIndex;
};

class SideSpace : public QFrame
{
    Q_OBJECT
public:
    SideSpace(QWidget* parent = nullptr) : QFrame(parent) {}
    ~SideSpace() {}
protected:
    void mousePressEvent(QMouseEvent* event) override
    {
        if (event->button() == Qt::LeftButton)
        {
            // 记录鼠标按下时的位置
            m_lastPos = event->globalPos();
            //setCursor(Qt::ClosedHandCursor);  // 启用后，鼠标悬停会改变鼠标的样式
        }
        QFrame::mousePressEvent(event);  
    }
    void mouseMoveEvent(QMouseEvent* event) override
    {
        if (event->buttons() & Qt::LeftButton)
        {
            // 计算移动距离
            QPoint delta = event->globalPos() - m_lastPos;
            // 移动窗口
            window()->move(window()->pos() + delta);
            // 更新位置
            m_lastPos = event->globalPos();
        }
        QFrame::mouseMoveEvent(event);
    }
private:
    QPoint m_lastPos;
};

class SideBar : public QFrame
{
    Q_OBJECT
public:
    SideBar(QWidget* parent = nullptr);
    virtual ~SideBar() {}
    void setBaseBackground(const QString strColour);
    void refresh();
    void addTabPage(const QString& strTitle, const QString& strIcon);

signals:
    void eventEnterStatus(bool);
    void itemSelected(uint, QString);

private slots:
    void slt_selected(uint nIndex, QString strTitle);

protected:
    bool eventFilter(QObject* obj, QEvent* event) override;

private:
    QString                     m_strBaseBackground;
    std::map<uint, OneItem*>    m_mpItems;
    SideSpace*                  m_pSideSpace;
};

// 多界面浏览窗口
class SideBrowser : public QFrame
{
    Q_OBJECT
public:
    SideBrowser(QWidget* parent = nullptr);
    virtual ~SideBrowser() {}

    void changeSideBar();
    void addTabPage(const QString& strTitle, const QString& strIcon, QWidget* pTab);
    void addTabPage(const QString& strTitle, const QString& strIcon, const FUNC_FORM_TYPE nType);
    void clostAutoExpand();
    void showTabIndex();
   
signals:
    void showPageTitle(QString);

private slots:
    void slt_eventEnterStatus(bool bStatus);
    void slt_selected(uint nIndex, QString strTitle);

protected:
    void resizeEvent(QResizeEvent* event) override;

private:
    void expand();
    void collapsed();

private:
    QPropertyAnimation* m_pAnimationBar;
    QPropertyAnimation* m_pAnimationTab;
    SideBar* m_pSidebar;
    QStackedWidget* m_pTabBrowser;

    uint m_SideWidth;
    bool m_bExpandStatus;
    bool m_bAutoExpand;
};

#endif // SIDEBROWSER_H
